import '../util/_base_export.dart';
import '../util/_view_export.dart';

class ProductBloc extends LoadingMoreBase<Map> with ChangeNotifier {
  Map _product_detail = {};
  Map get product_detail => _product_detail;

  List _sub_cate = [];
  List get sub_cate => _sub_cate;
  List _home_cate = [];
  List _promo_list = [];

  List get home_cate => _home_cate;
  List get promo_list => _promo_list;

  int pageindex = 1;
  bool _hasMore = true;
  bool forceRefresh = false;
  @override
  bool get hasMore => (_hasMore && length < AppConst.prodQueryMax) || forceRefresh;

  Map<String, dynamic> criteria = {
    "category_id": "",
  };

  void init() {
    _product_detail = {};
    _sub_cate = [];
  }

  void clearProd() {
    _product_detail = {};
  }

  void getHomeCategory() async {
    Map<String, dynamic> lm_param = {};
    var lv_wh_id = AppSession.wholesale_wh_id != 0 ? AppSession.wholesale_wh_id : AppSession.wh_id;
    lm_param = {"wh_id": lv_wh_id};
    print(' Product homw Cate call, pass wh_id ${lv_wh_id}');
    var lm_detail = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.prod_home,
      {},
      lm_param,
      false,
      true,
    );

    if ((lm_detail ?? {}).isNotEmpty) {
      if ((lm_detail['ev_result'] ?? {}).isNotEmpty)
        _home_cate = lm_detail['ev_result']['categories'];
      _promo_list = lm_detail['ev_result']['promos'];
      notifyListeners();
    }
  }

  void fetchDetail(int iv_prod_id) async {
    Map<String, dynamic> lm_param = {};

    lm_param = {"sku_id": iv_prod_id};

    var lm_detail = await AppSession.appReq.futureWrapper(
      ApiConst.httpPost,
      ApiServer.prod + ApiUri.prod_detail,
      {},
      lm_param,
      false,
      true,
    );
    print('images');
    print(lm_detail['ev_result']['detail_images']);
    var lm_return = {};
    var lm_images = [];
    if ((lm_detail ?? {}).isNotEmpty) {
      if ((lm_detail['ev_result'] ?? {}).isNotEmpty) {
        lm_return = lm_detail['ev_result'];
        if ((lm_detail['ev_result']['detail_images'] ?? '').isEmpty)
          lm_images = [];
        else
          lm_images = json.decode(lm_detail['ev_result']['detail_images']);
        if (lm_images.isEmpty)
          lm_return['detail_images'] = [];
        else
          lm_return['detail_images'] = lm_images;
        // lm_detail['ev_result']['detail_images'] =
        //     json.decode(lm_detail['ev_result']['detail_images']) ?? [];
      }
      _product_detail = lm_return; //lm_detail['ev_result'];
      notifyListeners();
    }
  }

  @override
  Future<bool> refresh([bool clearBeforeRequest = false]) async {
    _hasMore = true;
    pageindex = 1;
    //force to refresh list when you don't want clear list before request
    //for the case, if your list already has 20 items.
    forceRefresh = !clearBeforeRequest;
    var result = await super.refresh(clearBeforeRequest);
    forceRefresh = false;
    return result;
  }

  @override
  Future<bool> loadData([bool isloadMoreAction = false]) async {
    bool isSuccess = false;
    try {
      // //to show loading more clearly, in your app,remove this
      // await Future.delayed(Duration(milliseconds: 500));
      print('wh_id');
      print(AppSession.wh_id);
      Map<String, dynamic> lm_param = criteria;
      lm_param.addAll({
        "offset": (pageindex - 1) * AppConst.prodQueryPagesize,
        "limit": AppConst.prodQueryPagesize,
        "wh_id": AppSession.wholesale_wh_id != 0 ? AppSession.wholesale_wh_id : AppSession.wh_id
      });
      print(lm_param);
      var lm_result = await AppSession.appReq.futureWrapper(
        ApiConst.httpPost,
        ApiServer.prod + ApiUri.prod_list,
        {},
        lm_param,
        false,
        false,
      );
      var source = [];

      if ((lm_result ?? []).isNotEmpty) {
        print(lm_result);
        if (lm_result['ev_error'] == 0) {
          source = lm_result['ev_result']['prods'];
          // _sub_cate = lm_result['ev_result']['sub_categories'];
          // print(_sub_cate);
          notifyListeners();
        }
      }

      if (pageindex == 1) {
        this.clear();
      }
      for (var item in source) {
        if (!this.contains(item) && hasMore) this.add(item);
      }

      _hasMore = source.length != 0;
      pageindex++;
      isSuccess = true;
    } catch (exception, stack) {
      isSuccess = false;
      print(exception);
      print(stack);
    }
    return isSuccess;
  }
}
